package com.sdp.right.db.sys_page.service.impl;

import java.util.List;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdp.core.bizc.redis.RedisUtil;
import com.sdp.right.db.sys_page.mapper.SysPageMapper;
import com.sdp.right.db.sys_page.service.ISysPageService;
import com.sdp.right.entity.SysPage;
import com.sdp.right.service.RemoveSystemService;

import lombok.extern.slf4j.Slf4j;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zsp
 * @since 2023-10-31
 */
@Service
@Slf4j
@DS(value="right")
public class SysPageServiceImpl extends ServiceImpl<SysPageMapper, SysPage> implements ISysPageService,RemoveSystemService {
	
	@Autowired
	private RedisUtil redisUtil;
	
	@Value("${sdp.initSysPage}")
	private boolean initSysPage;
	
	@PostConstruct
	public void init() {
		//将页面放入缓存
		if(initSysPage) {
			redisUtil.del(SysPage.class);
			log.info("**********开始查询自定义页面，并将页面放入缓存中，请稍后************");
			List<SysPage> pageList = list();
			for(SysPage page : pageList) {
				redisUtil.setMap(SysPage.class, page.getKeyId(), page);
				log.info("**********将页面<<"+page.getPageName()+">>放入缓存");
			}
			log.info("**********将页面放入缓存结束*********");
		}
		
	}

	@Override
	public void removeSysPageByPageId(String keyId) {
		LambdaQueryWrapper<SysPage> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SysPage::getKeyId, keyId);
		remove(queryWrapper);
		
	}

	@Override
	public SysPage querySysPageById(String keyId) {
		LambdaQueryWrapper<SysPage> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SysPage::getKeyId, keyId);
		return getOne(queryWrapper,false);
	}

	@Override
	public void removeBySystemNo(String systemNo) {
		LambdaQueryWrapper<SysPage> queryWrapper = new LambdaQueryWrapper<>();
		queryWrapper.eq(SysPage::getSystemNo, systemNo);
		remove(queryWrapper);
		
	}

}
